import { useEffect } from "react";
import useGetUserInfo_redux from "./useGetUserInfo_redux";
import { useLocation, useNavigate } from "react-router-dom";
import { isLoginORRegister, MANAGE_INDEX_PATHNAME, isNoNeedUSerInfo, LOGIN_PATHNAME } from "../route";
function useNavPages(waittingData: boolean) {
  const { username } = useGetUserInfo_redux();
  const { pathname } = useLocation();
  const nav = useNavigate();
  useEffect(() => {
    if (waittingData) {
      return;
    }
    //表示已经登录 判断当前的目录是否是登录页或者注册页
    if (username) {
      if (isLoginORRegister(pathname)) {
        //已经登录的画就跳转到列表页
        nav(MANAGE_INDEX_PATHNAME);
      }
      return;
    }
    //未登录
    if (isNoNeedUSerInfo(pathname)) {
      return;
    } else {
      nav(LOGIN_PATHNAME);
    }
  }, [waittingData]);
}

export default useNavPages;
